package com.risen.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @auther lmj
 * @date 2019/8/7
 */


@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoJDBC {
    @Test
    public void jdbcTest(){

        try {
            // 1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获取连接
            Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
            // 3.获取执行语句
            String sql = "SELECT * FROM USER";
            PreparedStatement pst = connection.prepareStatement(sql);
            // 4.执行语句
            ResultSet res = pst.executeQuery();
            // 5.处理结果集
            while(res.next()){
                String username = res.getString("username");
                String password = res.getString("password");
                System.out.println(username +"=========================="+ password);
            }
            // 6.释放资源
            res.close();
            pst.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
